//
//  MultipleLinesChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/6.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "MultipleLinesChartViewController.h"
@interface MultipleLinesChartViewController ()
@end

@implementation MultipleLinesChartViewController

LineChartView *sampleMultipleLinesChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initLineChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initLineChartView{
    sampleMultipleLinesChartView=[[LineChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleMultipleLinesChartView.backgroundColor = [UIColor whiteColor];
    sampleMultipleLinesChartView.delegate = self;
    [self.view addSubview:sampleMultipleLinesChartView];
    [self initLineChartStyleAndData];
}

-(void)initLineChartStyleAndData{
    [self setupBarLineChartView:sampleMultipleLinesChartView];
    [self setLineChartParam:sampleMultipleLinesChartView];
    [self setLegendStyle:sampleMultipleLinesChartView];
    [self initChartData];
}

- (void)setupBarLineChartView:(BarLineChartViewBase *)chartView
{
    //描述
    chartView.descriptionText = @"descriptionText";
    //无数据默认显示
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    //网格背景
    chartView.drawGridBackgroundEnabled = NO;
    chartView.drawBordersEnabled = YES;
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.pinchZoomEnabled = NO;
    // ChartYAxis *leftAxis = chartView.leftAxis;
    //x轴
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    chartView.rightAxis.enabled = NO;
}

-(void)setLineChartParam:(LineChartView *)chartView{
    chartView.leftAxis.enabled = NO;
    chartView.rightAxis.drawAxisLineEnabled = NO;
    chartView.rightAxis.drawGridLinesEnabled = NO;
    chartView.xAxis.drawAxisLineEnabled = NO;
    chartView.xAxis.drawGridLinesEnabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(LineChartView *)chartView{
    
   //图例在柱状图中的位置
    chartView.legend.position = ChartLegendPositionRightOfChart;
    chartView.legend.form = ChartLegendFormLine;
}

- (void)initChartData
{
    [self setDataCount:19 range:10 chart:sampleMultipleLinesChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(LineChartView *)chartView
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i) stringValue]];
    }
    
    NSArray *colors = @[ChartColorTemplates.vordiplom[0], ChartColorTemplates.vordiplom[1], ChartColorTemplates.vordiplom[2]];
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    
    for (int z = 0; z < 3; z++)
    {
        NSMutableArray *values = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < count; i++)
        {
            double val = (double) (arc4random_uniform(range) + 3);
            [values addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
        }
        
        LineChartDataSet *d = [[LineChartDataSet alloc] initWithYVals:values label:[NSString stringWithFormat:@"DataSet %d", z + 1]];
        d.lineWidth = 2.5;
        d.circleRadius = 4.0;
        
        UIColor *color = colors[z % colors.count];
        [d setColor:color];
        [d setCircleColor:color];
        [dataSets addObject:d];
    }
    
    ((LineChartDataSet *)dataSets[0]).lineDashLengths = @[@5.f, @5.f];
    ((LineChartDataSet *)dataSets[0]).colors = ChartColorTemplates.vordiplom;
    ((LineChartDataSet *)dataSets[0]).circleColors = ChartColorTemplates.vordiplom;
    
    LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
    chartView.data = data;
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end